﻿
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using Chaotic.Core.Extension;
using Chaotic.Module.DBTool;
using Chaotic.Module.General;
using DryIoc;
using Prism.Ioc;
using Prism.Modularity;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.SplashScreen;

namespace ChaoticApplication
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App 
    {
        protected override Window CreateShell()
        { 
            return Container.Resolve<MainWindow>();
        }

        protected override async void OnInitialized()
        {
            // 注册全局异常处理器
            DispatcherUnhandledException += GlobalUiExceptionHandler;
            ////指定加载程序集的路径
            //AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

            ShowSplashScreen();

            //加载模块
            await Task.Delay(3000); 
            RadSplashScreenManager.Close();

            base.OnInitialized();
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            //containerRegistry.RegisterSingleton<IMessageService, MessageService>();
        }

        protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
        {
            moduleCatalog.AddModule<GeneralModule>();
            moduleCatalog.AddModule<DBToolModule>(); 
        }

 
        private void GlobalUiExceptionHandler(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            MessageBoxExtension.ShowError(e.Exception.Message); 
        }

        //private Assembly? CurrentDomain_AssemblyResolve(object? sender, ResolveEventArgs args)
        //{
        //    string assemblyName = new AssemblyName(args.Name).Name;
        //    string assemblyPath = AppDomain.CurrentDomain.BaseDirectory + "\\Modules\\DB\\" + assemblyName + ".dll";
        //    if (File.Exists(assemblyPath))
        //    {
        //        return Assembly.LoadFrom(assemblyPath);
        //    }
        //    return null;
        //}



        private void ShowSplashScreen()
        {
            var dataContext = (SplashScreenDataContext)RadSplashScreenManager.SplashScreenDataContext;
            dataContext.ImagePath = "/ChaoticApplication;component/Images/splashscreen.jpg";
            dataContext.ImageHeight = 200;
            dataContext.ImageWidth = 500;
            dataContext.ImageStretch = System.Windows.Media.Stretch.Fill;
            dataContext.Content = "正在加载中，请稍后……";
            dataContext.Footer = "© 2025-2028 Lzqsky. All Rights Reserved."; 
            RadSplashScreenManager.Show(); 
        }
    }
}
